﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace sourceFileAnalyze
{
    public partial class Form1 : Form
    {
        string filepath;
        int originalLines = 0;
        int originalWords = 0;
        int validLines = 0;
        int validWords = 0;
        Dictionary<string,int> allWords = new Dictionary<string,int>();
        public Form1()
        {
            InitializeComponent();



        }
        //点击选择待分析文件
        private void buttonSelect_Click(object sender, EventArgs e)
        {
            //文件选择框
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "文本文件|*.cs";
            DialogResult result = dialog.ShowDialog();
            if(result==DialogResult.OK)
            {
                filepath = dialog.FileName;
                this.labelSelect.Text = dialog.FileName;
                    
              }
        }
        //显示原始行数
        private void buttonOriginalLines_Click(object sender, EventArgs e)
        {
            using(StreamReader rd=new StreamReader(filepath))
            {
                string line;
                while((line=rd.ReadLine())!=null)
                {
                    originalLines++;
                }

            }
            labelOriginalLines.Text = originalLines.ToString();
        }
        //显示原始词数
        private void buttonOriginalWords_Click(object sender, EventArgs e)
        {
            using (StreamReader rd = new StreamReader(filepath))
            {
                //正则匹配单词;
                string pattern = @"\b\w+\b";
                string line;
                MatchCollection matches;
                while ((line = rd.ReadLine()) != null)
                {
                    matches = Regex.Matches(line, pattern);
                    originalWords += matches.Count;
                }
                labelOriginalWords.Text = originalWords.ToString();

            }
        }
        //格式化后的行数
        private void buttonValidLines_Click(object sender, EventArgs e)
        {
            using (StreamReader rd = new StreamReader(filepath))
            {
                string line;
                while ((line = rd.ReadLine()) != null)
                {
                    if(line.Length >= 2 && line.Substring(0, 2) != "\\")
                    {
                        validLines++;
                    }

                }

            }
            labelValidLines.Text = validLines.ToString();
        }
        //有效词数
        private void buttonValidWords_Click(object sender, EventArgs e)
        {
            using (StreamReader rd = new StreamReader(filepath))
            {
                //正则匹配单词;
                string pattern = @"\b\w+\b";
                string line;
                MatchCollection matches;
                while ((line = rd.ReadLine()) != null)
                {
                    if (line.Length >= 2 && line.Substring(0, 2) != "\\")
                    {
                        matches = Regex.Matches(line, pattern);
                        validWords += matches.Count;
                       
                    }
                }
                labelValidWords.Text = validWords.ToString();

            }
        }

        private void buttonAllWords_Click(object sender, EventArgs e)
        {
            using (StreamReader rd = new StreamReader(filepath))
            {
                //正则匹配单词;
                string pattern = @"\b\w+\b";
                string line;
                MatchCollection matches;
                while ((line = rd.ReadLine()) != null)
                {
                    if (line.Length >= 2 && line.Substring(0, 2) != "\\")
                    {
                        matches = Regex.Matches(line, pattern);
                       foreach(Match match in matches )
                        {
                            string temp = match.Value;
                            if(allWords.ContainsKey(temp))
                            {
                                allWords[temp]++;
                            }
                            else
                            {
                                allWords.Add(temp, 1);
                            }
                        }

                    }
                }
                
            }
            //此时，allWord中存储这所有单词及其数量
            foreach(var kv in allWords)
            {
                string word = kv.Key;
                int number = kv.Value;
                ListViewItem item = new ListViewItem(word);
                item.SubItems.Add(number.ToString());
                listViewWords.Items.Add(item);
            }
            listViewWords.Visible = true;

        }
    }
}
